<?php
include("utils.php");
include("lang_buildCustomNetwork.php");
ob_start();
session_start();
if(!isset($_SESSION[siteuserId]))
echo '<script>location.href="index.php"</script>';

main_content("mainBuySpace();");
//mainBuySpace();
function mainBuySpace()
{
    if($_SESSION['langId']!="")
    {
            $langId=$_SESSION['langId'];
    }
    else
    {
            $langId="1";
    }
    if(isset($_POST['help_insert_main']))
    {
        //Saving Campaign
        $query_campaign = "insert into adcampaign(camp_name,spend_memo,total_budget,daily_budget,start_date,end_date,start_hour,end_hour,siteuserId,description) values('".$_POST['camp_name']."','".$_POST['spend_memo']."','".$_POST['total_budget']."','".$_POST['daily_budget_main']."','".$_POST['start_date']."','".$_POST['end_date']."','".$_POST['start_date_hour']."','".$_POST['end_date_hour']."','".$_SESSION[siteuserId]."','".$_POST['camp_desc']."') ";
        mysql_query($query_campaign);
        $insert_id = mysql_insert_id(); // just Inserted Campaign Id

        // Start First adgroup's network insert from temp_create_network table
        $temp_query = "select * from temp_create_network where identity = 'main_adgroup' ";
        $temp_res   = mysql_query($temp_query);
        $temp_row   = mysql_fetch_array($temp_res);
        if(mysql_num_rows($temp_res)>0)
        {
            $create_network_query = "INSERT INTO create_network(`ad_type`, `bid`, `per`, `banner_size`, `category`, `country`, `userid`) values('".$temp_row['ad_type']."','".$temp_row['bid']."','".$temp_row['per']."','".$temp_row['banner_size']."','".$temp_row['category']."','".$temp_row['country']."','".$temp_row['userid']."') ";
            $create_network_res = mysql_query($create_network_query);
            $create_network_insertid = mysql_insert_id();
            $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type,RON) values('".$_POST['add_gr_name']."','".$_POST['daily_budget']."','".$create_network_insertid."','".$_SESSION[siteuserId]."','".$insert_id."','custom','".$temp_row['RON']."') ";
        }
        else{
            $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type) values('".$_POST['add_gr_name']."','".$_POST['daily_budget']."','".$_POST['network']."','".$_SESSION[siteuserId]."','".$insert_id."','direct') ";
        }

        $res_campaign_adgroup = mysql_query($query_campaign_adgroup);
        $adgroup_insert_id = mysql_insert_id();
        $formid = 'createad';
        $query_ad = "select * from temp_adgroup_ad where form_id = '".$formid."' ";
        $res_ad = mysql_query($query_ad);

        while($row_ad = mysql_fetch_array($res_ad))
        {
                $insert = "insert into adgroup_ad(`adgroup_id`, `heading`, `ad_description`, `dest_url`, `personal_reference`, `campaign_id`, `upload_image`) values('".$adgroup_insert_id."','".$row_ad['heading']."','".$row_ad['ad_description']."','".$row_ad['dest_url']."','".$row_ad['personal_reference']."','".$insert_id."','".$row_ad['upload_image']."') ";
                mysql_query($insert);
        }

        // End First adgroup's network insert from temp_create_network table
        $total = $_POST['TOTAL_FILE'];

        for($i=1;$i<$total;$i++)
        {
            if(isset($_POST['add_gr_name'.$i]) && $_POST['add_gr_name'.$i] != ''){
            // Start Dynamic adgroup's network insert from temp_create_network table
            $temp_query = "select * from temp_create_network where identity = '".$i."' ";
            $temp_res   = mysql_query($temp_query);
            $temp_row   = mysql_fetch_array($temp_res);

            if(mysql_num_rows($temp_res)>0)
            {
                $create_network_query = "INSERT INTO create_network(`ad_type`, `bid`, `per`, `banner_size`, `category`, `country`, `userid`) values('".$temp_row['ad_type']."','".$temp_row['bid']."','".$temp_row['per']."','".$temp_row['banner_size']."','".$temp_row['category']."','".$temp_row['country']."','".$temp_row['userid']."') ";

                $create_network_res = mysql_query($create_network_query);
                $create_network_insertid = mysql_insert_id();
                // End Dynamic adgroup's network insert from temp_create_network table
                $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type,RON) values('".$_POST['add_gr_name'.$i]."','".$_POST['daily_budget'.$i]."','".$create_network_insertid."','".$_SESSION[siteuserId]."','".$insert_id."','custom','".$temp_row['RON']."') ";
            }
            else
            {
                $query_campaign_adgroup = "insert into adgroup(adgroup_name,adgroup_daily_budget,adgroup_network,siteuserId,campaign_id,network_type) values('".$_POST['add_gr_name'.$i]."','".$_POST['daily_budget'.$i]."','".$_POST['network'.$i]."','".$_SESSION[siteuserId]."','".$insert_id."','direct') ";
            }

            $res_campaign_adgroup = mysql_query($query_campaign_adgroup);

            $adgroup_insert_id = mysql_insert_id();

            $formid = 'createad'.$i;
            $query_ad = "select * from temp_adgroup_ad where form_id = '".$formid."' ";
            $res_ad = mysql_query($query_ad);

            while($row_ad = mysql_fetch_array($res_ad))
            {
                $insert = "insert into adgroup_ad(`adgroup_id`, `heading`, `ad_description`, `dest_url`, `personal_reference`, `campaign_id`, `upload_image`) values('".$adgroup_insert_id."','".$row_ad['heading']."','".$row_ad['ad_description']."','".$row_ad['dest_url']."','".$row_ad['personal_reference']."','".$insert_id."','".$row_ad['upload_image']."')";

                mysql_query($insert);
            }
                                                                          }

        }

        ############# code by xxxx starts######
        $sql_empty_temp_adgroup_ad="delete from temp_adgroup_ad";
        $exe_empty_temp_adgroup_ad=mysql_query($sql_empty_temp_adgroup_ad);

        $sql_empty_temp_create_network="delete from temp_create_network";
        $exe_empty_temp_create_network=mysql_query($sql_empty_temp_create_network);
        echo '<script>window.location="advertiser_custom_statistics.php"</script>';
}


// Start Retrieve network List
$sqlNetworkAds = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";
$execNetworkAds = mysql_query($sqlNetworkAds);
$execNetworkAds1 = mysql_query($sqlNetworkAds);
// End Retrieve network List

$optionstring='';
while($fetNetworkAds1 = mysql_fetch_array($execNetworkAds1))
{
    $temp=$fetNetworkAds1[networkId]."-".$fetNetworkAds1[networkType];
    $optionstring .= '<option value='.$temp.'>'.strtoupper($fetNetworkAds1['networkType']).'-'.ucfirst($fetNetworkAds1['networkName']).' '.'$'.$fetNetworkAds1['networkMinPrice'].'</option>';

}
?>



<script type="text/javascript">
        var GB_ROOT_DIR = "./greybox/";
</script>
<script type="text/javascript" src="greybox/AJS.js"></script>
<script type="text/javascript" src="greybox/AJS_fx.js"></script>
<script type="text/javascript" src="greybox/gb_scripts.js"></script>
<link href="greybox/gb_styles.css" rel="stylesheet" type="text/css" media="all" />

    <script type="text/javascript" src="static_files/help.js"></script>
    <link href="static_files/help.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript">
    //insertHeader();
    //insertMenu('examples');
</script>

<!-- Links For JS CALENDER   -->
<link type="text/css" rel="stylesheet" href="dhtml_calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css?random=20051112" media="screen" />
<script type="text/javascript" src="dhtml_calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.js?random=20060118"></script>

<!-- End line for JS CALENDER -->
<script type="text/javascript">
var gFiles = 1;
window.onunload = delete_garbage_adgrp();

function delete_garbage_adgrp()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp = new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {

delete_garbage_network();
//alert(xmlHttp.responseText);
      }
    }

  xmlHttp.open("GET","delete_garbage_adgroup.php",true);
  xmlHttp.send(null);
  return false;
}


function delete_garbage_network()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
	//alert(xmlHttp.responseText);
      }
    }

  xmlHttp.open("GET","delete_garbage_network.php",true);
  xmlHttp.send(null);
  return false;
}




function make_div(gFiles)
{
    var networkId_type=document.getElementById("network"+gFiles).value;
    //alert(networkId_type);
    if(networkId_type=='')
    {
        document.getElementById('show_campaign_network'+gFiles).style.display='';
        return false;
    }
    else
    {
        var network_details = networkId_type.split("-");
        /*********8 ************/
        if(document.getElementById('net_id'+gFiles))
        {
            var net_id = document.getElementById('net_id'+gFiles).value.split('-');
            if(net_id[0]==network_details[0])
            {
                GB_showCenter('Enter Your Ad','../advertiser_direct_popup.php?fid='+gFiles+'&form_id=createad'+ gFiles +'&network_id='+network_details[0]+'&network_type='+network_details[1],500,800);
            }
            else
            {
                alert('You have chosen wrong one');
                return false;
            }
        }
        else
        {
        GB_showCenter('Enter Your Ad','../advertiser_direct_popup.php?fid='+gFiles+'&form_id=createad'+ gFiles +'&network_id='+network_details[0]+'&network_type='+network_details[1],500,800);
        }
    }
}

var iGroup =1;

function removeDisplay(){
    if(iGroup==1){
        var rm = document.getElementById('cRemove');
        rm.style.display = "none";
    }
    else{
        var rm = document.getElementById('cRemove');
        rm.style.display = "";
    }
}

function adddiv(optionstring)
{
    iGroup += 1;
    var tr = parent.document.createElement("tr");
    tr.setAttribute("id", "adgroup" + gFiles);
    var td = parent.document.createElement("td");

    maintdbody =document.getElementById('adgroup').value;
    maintdbody = "<table id='createad"+gFiles+"' width='100%' name='createad"+gFiles+"'><tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td><table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=11><img src=images/PinkBorderTopLeft.jpg width=11 height=11 /></td><td class=PinkBoxTopBg><img src=images/spacer.gif width=10 height=11 /></td><td width=11><img src=images/PinkBorderTopRight.jpg alt= width=11 height=11 /></td></tr></table></td></tr><tr><td class=PinkTwosideBorder><table width=100% border=0 align=center cellpadding=0 cellspacing=0><tr><td>&nbsp;<table width=100%><tr><td width=23>&nbsp;</td><td width=435 id=ad_type_var"+gFiles+"><strong>Ad Type:</strong> Text</td><td width=749> <strong>Units:</strong> CPC</td></tr><tr><td>&nbsp;</td><td><strong>Ad Group Name</strong></td><td><input type=text name=add_gr_name"+gFiles+" id=add_gr_name"+gFiles+"></td></tr><tr> <td></td><td></td><td id=validate_add_gr_name"+gFiles+" style=display:none;></td></tr>\n\
                <tr><td>&nbsp;</td><td><strong>Daily Budget</strong></td><td><input type=text name=daily_budget"+gFiles+" id=daily_budget"+gFiles+"></td></tr><tr><td>&nbsp;</td><td><strong>Network</strong></td><td style=width:1em;nowrap:nowrap; id=adgroup_network"+gFiles+">\n\
                <select name=network"+gFiles+" id=network"+gFiles+" onchange='return custom_check("+gFiles+")';><option value=''>Select Network</option><option value=custom>Custom</option>";
    maintdbody +=optionstring;
    maintdbody +="</select></td></tr><tr><td></td><td></td><td id=validate_network style=display:none;></td></tr><tr><td></td><td></td><td id=main_adgroup_save style=display:none;></td></tr><tr><td id=CreateAdcreatead"+gFiles+" colspan=3 align=center></td></tr><tr><td></td><td></td><td><input type=button name=create_ad"+gFiles+" id=create_ad"+gFiles+" value='Create Ad' onclick=make_div("+gFiles+")></td></tr>";
    maintdbody +='<tr id=cRemove><td align="right" colspan=3><a href="javascript:void(0);" onclick="deleteRow('+gFiles+');">Remove</a>&nbsp;&nbsp;&nbsp;</td></tr>';
    maintdbody +="<tr id='show_campaign_network"+gFiles+"' style='display:none'><td colspan=3 align=center>Please Select The Network</td></tr></table></td></tr></table></td></tr><tr><td><table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=11><img src=images/PinkBorderBottomLeft.jpg width=11 height=11 /></td><td class=PinkBoxBottomBg><img src=images/spacer.gif width=10 height=11 /></td><td width=11><img src=images/PinkBorderBottomRight.jpg width=11 height=11 /></td></tr></table></td></tr></table></td></tr></table>";
    td.innerHTML = maintdbody;
    tr.appendChild(td);
    parent.document.getElementById("adgroup").appendChild(tr);
    gFiles++;
    document.getElementById("TOTAL_FILE").value=gFiles;
    document.getElementById("no_of_adGroups").innerHTML="Ad Groups:"+ gFiles;    
    removeDisplay();
}

//function delete row
function deleteRow(tableId)
{
    try
    {
        var tbl = document.getElementById('adgroup');
        var tr = document.getElementById('adgroup'+tableId);
        tbl.removeChild(tr);
        gFiles--;
        document.getElementById("no_of_adGroups").innerHTML="Ad Groups:"+ gFiles;
        iGroup -= 1;
        removeDisplay();
    }
    catch(e)
    {
        alert(e);
    }

}

function validate_sub_adgroup(id)
{
    var ad_main_group = document.getElementById("add_gr_name"+id).value;
    var network = document.getElementById("network"+id).value;
    if(network != "Select Network"){
        document.getElementById("validate_network"+id).style.display="none";
    }
    if(ad_main_group != ""){
        document.getElementById("validate_add_gr_name"+id).style.display="none";
    }
    if(ad_main_group == ""){
        document.getElementById("validate_add_gr_name"+id).style.display="block";
        document.getElementById("validate_add_gr_name"+id).innerHTML="Please Enter Ad Group Name!";
        document.getElementById("add_gr_name"+id).focus();
        return false;
    }
    if(network == "Select Network"){
        document.getElementById("validate_network"+id).style.display="block";
        document.getElementById("validate_network"+id).innerHTML="Please Enter Network!";
        document.getElementById("network"+id).focus();
        return false;
    }
    ading_adgroup(id);
    return true;
}

function removeFile(aId)
{
    var obj = document.getElementById(aId);
    obj.parentNode.removeChild(obj);
}

function ading_adgroup(div_id)
{
	var div_id = div_id;
	var ad_gr_name = document.getElementById('add_gr_name'+div_id).value;
	var dail_bud = document.getElementById('daily_budget'+div_id).value;
	var network = document.getElementById('network'+div_id).value;
	var xmlHttp;
	try
	{
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
            // Internet Explorer
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
	}
	xmlHttp.onreadystatechange=function()
        {
            if(xmlHttp.readyState==4)
            {
                //SOMETHING HERE
            }
        }
	xmlHttp.open("GET","adgroup_ajax.php?ad_gr_name="+ad_gr_name+"&dail_bud="+dail_bud+"&network="+network,true);
	xmlHttp.send(null);
	return false;
}

function validate_main_adgroup()
{
	var ad_main_group = document.getElementById("add_gr_name").value;
	var network = document.getElementById("network").value;
	if(network != "Select Network"){
        document.getElementById("validate_network").style.display="none";
    }

	if(ad_main_group != ""){
        document.getElementById("validate_add_gr_name").style.display="none";
    }

	if(ad_main_group == ""){
        document.getElementById("validate_add_gr_name").style.display="block";
        document.getElementById("validate_add_gr_name").innerHTML="Please Enter Ad Group Name!";
        document.getElementById("add_gr_name").focus();
        return false;
    }

	if(network == "Select Network"){
        document.getElementById("validate_network").style.display="block";
        document.getElementById("validate_network").innerHTML="Please Enter Network!";
        document.getElementById("network").focus();
        return false;
    }

	add_main();
	return true;
}

function validate_main_campaign()
{
    if(document.create_campaign.camp_name.value == ""){
        document.getElementById("validate_camp_name").style.display="block";
        document.getElementById("camp_name").focus();
        return false;
    }else{
    	document.getElementById("validate_camp_name").style.display="none";
    }

    if(document.getElementById("camp_desc").value == "")
    {
        document.getElementById("validate_camp_desc").style.display="block";
        document.getElementById("camp_desc").focus();
        return false;
    }
    else
    {
        document.getElementById("validate_camp_desc").style.display="none";
    }

    if(document.getElementById("total_budget").value == ""){
        document.getElementById("validate_total_budget").style.display="block";
        document.getElementById("total_budget").focus();
        return false;
    }
    else{
    	document.getElementById("validate_total_budget").style.display="none";
    }

    if(document.getElementById("daily_budget_main").value == ""){
        document.getElementById("validate_daily_budget_main").style.display="block";
        document.getElementById("daily_budget_main").focus();
        return false;
    }
    else{
    	document.getElementById("validate_daily_budget_main").style.display="none";
    }

	if(document.getElementById("start_date").value == ""){
        document.getElementById("validate_start_date").style.display="block";
        document.getElementById("start_date").focus();
        return false;
    }else{
    	 document.getElementById("validate_start_date").style.display="none";
    }

    if(document.getElementById("charges_amt").value == ""){
        document.getElementById("validate_charges_amt").style.display="block";
        document.getElementById("charges_amt").focus();
        return false;
    }else{
    	document.getElementById("validate_charges_amt").style.display="none";
    }

    /////////////////Start of Ad Group validation
    var ad_main_group = document.getElementById("add_gr_name").value;
    var network = document.getElementById("network").value;
    if(network != "Select Network"){
        document.getElementById("validate_network").style.display="none";
    }
	if(ad_main_group != ""){
        document.getElementById("validate_add_gr_name").style.display="none";
    }

	if(ad_main_group == ""){
        document.getElementById("validate_add_gr_name").style.display="block";
        document.getElementById("validate_add_gr_name").innerHTML="Please Enter Ad Group Name!";
        document.getElementById("add_gr_name").focus();
        return false;
    }

   if(network == "Select Network"){
        document.getElementById("validate_network").style.display="block";
        document.getElementById("validate_network").innerHTML="Please Enter Network!";
        document.getElementById("network").focus();
        return false;
    }
    for(i=1;i<gFiles;i++)
    {
        if(document.getElementById("createad"+i))
        {
            var ad_main_group = document.getElementById("add_gr_name"+i).value;
            var network = document.getElementById("network"+i).value;
            if(ad_main_group != ""){
                    document.getElementById("validate_add_gr_name"+i).style.display="none";
            }

            if(network != "Select Network"){
                    document.getElementById("validate_network"+i).style.display="none";
            }

            if(ad_main_group == ""){
                document.getElementById("validate_add_gr_name"+i).style.display="block";
                document.getElementById("validate_add_gr_name"+i).innerHTML="Please Enter Ad Group Name!";
                document.getElementById("add_gr_name"+i).focus();
                return false;
            }

            if(network == "Select Network"){
                document.getElementById("validate_network"+i).style.display="block";
                document.getElementById("validate_network"+i).innerHTML="Please Enter Network!";
                document.getElementById("network"+i).focus();
                return false;
            }
        }
    }
    // End of Ad Group validation /////////////////
    // Start Same Ad Group Same name validation

    for(var j=1;j<gFiles;j++)//For main Adgroup
    {
        if(document.getElementById("createad"+j)){
            if(document.getElementById("add_gr_name").value == document.getElementById("add_gr_name"+j).value)
            {
                alert('Adgroup Name should be unique for a Campaign');
                document.getElementById("add_gr_name"+j).focus();
                return false;
            }
        }
    }

    for(i=1;i<gFiles;i++)//For dynamic Adgroup
    {
        for(j=1;j<gFiles;j++)//For dynamic Adgroup
        {
            if(document.getElementById("createad"+i)){
                if(document.getElementById("createad"+j)){
                    if(i!=j){
                        if(document.getElementById("add_gr_name"+i).value == document.getElementById("add_gr_name"+j).value)
                        {
                            alert('Adgroup Name should be unique for a Campaign');
                            document.getElementById("add_gr_name"+j).focus();
                            return false;
                        }
                    }
                }
            }
        }
    }


    // Numeric Validation

    if (IsNumeric(document.getElementById('total_budget').value) == false)
    {
        alert("Please enter Numeric field only in Total Budget");
        document.getElementById('total_budget').focus();
        return false;
    }

    if (IsNumeric(document.getElementById('daily_budget_main').value) == false)
    {
        alert("Please enter Numeric field only in Daily Budget");
        document.getElementById('daily_budget_main').focus();
        return false;
    }

    // End of numeric Validation

    // Start Greater value of Total budget validation

    if(document.getElementById('daily_budget_main').value > document.getElementById('total_budget').value )
    {
        alert('Please enter Daily Budget less than Total Budget');
        document.getElementById('daily_budget_main').focus();
        return false;
    }

    // end Greater value of Total budget validation
    // Start Same Ad Group name validation

    create_campaign.submit();

}



function IsNumeric(strString)
   //  check for valid numeric strings
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


function custom_check(identity)
{
if(identity == 'main_adgroup')
{
	var networkDetails=document.getElementById('network').value;
}
else
{
	var networkDetails=document.getElementById('network'+identity).value;
}
var networkDetails_arr=networkDetails.split("-");

//var identity = identity;
    if(identity == 'main_adgroup')
    {
        if(document.getElementById('network').value == 'custom') {
            GB_showCenter('Create Custom network','../createbuildCustomNetwork.php?form_id=main_adgroup',500,800);
        }
        else{
            var ct = document.getElementById('rCountry');
            if(networkDetails_arr[0]==<?=iPhoneAppID?> || networkDetails_arr[0]==<?=iPhoneWebID?> || networkDetails_arr[0]==<?=iPhoneMapID?> || networkDetails_arr[0]==<?=iTunesID?>){
                ct.style.display = '';
            }
            else{
                ct.style.display = 'none';
            }
            document.getElementById('ad_type_var').style.display='';
            document.getElementById('ad_type_var').innerHTML="<strong>Ad Type:</strong>"+ networkDetails_arr[1];
        }
    }
    else
    {
        if(document.getElementById('network'+identity).value == 'custom') {
        GB_showCenter('Create Custom network','../createbuildCustomNetwork.php?form_id='+identity,500,800);
        }
        else
        {
            document.getElementById('ad_type_var'+identity).style.display='';
            document.getElementById('ad_type_var'+identity).innerHTML="<strong>Ad Type:</strong>"+ networkDetails_arr[1];
        }
    }
}
</script>

<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajaxresponse.js"></script>
<script type="text/javascript">
function delete_ad(temp_ad_id,formId)
{
    sendRequest('ajax/ajax_delete_ad.php','ad_id='+temp_ad_id+'&formId='+formId,'POST');
}
</script>
<script type="text/javascript" src="Javascripts/registration.js"></script>
<link href="css/global.css" rel="stylesheet" type="text/css">
<script type="text/javascript" type="text/javascript" src="Javascripts/datetimepicker.js"></script>

<script type="text/javascript">
function winpopup()
{
    var networkId_type=document.getElementById("network").value;
    if(networkId_type=='')
    {
        document.getElementById('show_campaign_network').style.display='';
        return false;
    }
    else
    {
        var network_details = networkId_type.split("-");
        if(document.getElementById('net_id'))
        {
                var net_id = document.getElementById('net_id').value;
                if(net_id==network_details[0])
                {
                    GB_showCenter('Enter Your Ad','../advertiser_direct_popup.php?form_id=createad&network_id='+network_details[0]+'&network_type='+network_details[1],500,800);
                }
                else
                {
                    alert('Please Select The Previous Network');
                    return false;
                }
        }
        else
        {
            GB_showCenter('Enter Your Ad','../advertiser_direct_popup.php?form_id=createad&network_id='+network_details[0]+'&network_type='+network_details[1],500,800);
        }
    }
}
</script>

<input type="hidden" name="mode" value="UserRegistration" />
<!-- begin -->
<table width="95%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td valign="top" >
    <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td width="20%" valign="top"><?php //include("left_pannel.php"); ?></td>
            <td width="10px">&nbsp;</td>
            <td valign="top">
                <table width="98%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td valign="top" align="left">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td width="11" valign="top" align="left"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                    <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                    <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                    </tr>
                    </table>
                </td>
                </tr>
                <tr>
                <td class="PinkTwosideBorder" valign="top">
                <form name="create_campaign" method="POST" id="create_campaign" action="">
                <input name="help_insert_main" id="help_insert_main" value="help_insert_main" type="hidden">
                <input name="TOTAL_FILE" id="TOTAL_FILE" value="1" type="hidden">
                <table border="0" width="100%">
                <tr>
                <td width="50%" align="center" valign="top">
                <table>
                <tr><td><b><font size="2">Campaign Details</font></b></td></tr>
                <tr>
                <td>Name</td>
                <td><input type="text" name="camp_name" id="camp_name" /></td>
                </tr>
                <tr>
                <td></td>
                <td style="display:none;" id="validate_camp_name">Please Enter the name</td>
                </tr>
                <tr>
                                <td valign="top">Description</td>
                                <td><textarea name="camp_desc" id="camp_desc" style="width:145px;height:50px;"></textarea></td>
                        </tr>
                        <tr>
                                <td></td>
                                <td style="display:none;" id="validate_camp_desc">Please Enter the description.</td>
                        </tr>
                <tr>
                <td>Spend Memo</td>
                <td align="left">
                <table align="left">
                <tr><td><input type="radio" name="spend_memo" value="fast_possible" checked="true"></td>
                <td>Run ads as fast as possible</td>
                </tr>
                <tr><td><input type="radio" name="spend_memo" value="all_day"></td><td>Run ads throughout the day</td></tr>
                </table>
                </td>
                </tr>
                <tr>
                <td>Total Budget    $   </td>
                <td><inpUT type="text" name="total_budget" id="total_budget"></td>
                </tr>
                <tr>
                <td></td>
                <td style="display:none;" id="validate_total_budget">Please Enter Total Budget</td>
                </tr>
                <tr>
                <td>daily Budget</td>
                <td><inpUT type="text" name="daily_budget_main" id="daily_budget_main"></td>
                </tr>
                <tr>
                <td></td>
                <td style="display:none;" id="validate_daily_budget_main">Please Enter Daily Budget</td>
                </tr>
                <tr>
                <td>Start Date</td>
                <td>
                <table>
                <tr>
                <td>
                <!--<input type="text" name="start_date" id="start_date" onclick="displayCalendar(document.create_campaign.start_date,'yyyy/mm/dd',this)">-->
                <input type="text" name="start_date" id="start_date" value="<?php echo $previousyear; ?>" onClick="javascript:NewCal('start_date','yyyymmdd',false,24);">
                <!--<inpUT type="text" name="start_date" id="start_date" />-->
                </td>
                <td>Hour
                <select name="start_date_hour">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                </select>
                </td>
                </tr>
                </table>
                </td>
                </tr>

                <tr>
                <td></td>
                <td style="display:none;" id="validate_start_date">Please Enter Start Date</td>
                </tr>
                <tr>
                <td>End Date</td>
                <td>
                    <table width="100%">
                        <tr>
                        <td>
                            <!--<input type="text" name="end_date" id="end_date" onclick="displayCalendar(document.create_campaign.end_date,'yyyy/mm/dd',this)">-->
                            <input type="text" name="end_date" id="end_date" value="<?php echo $nextyear; ?>" onclick="javascript:NewCal('end_date','yyyymmdd',false,24);">
                            <!--<inpUT type="text" name="start_date" id="start_date" />-->
                        </td>
                            <td>Hour
                            <select name="end_date_hour">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                            </select>
                            </td>
                        </tr>
                    </table>
                </td>
                </tr>
            </table>
            </td>
            <td width="50%" align="center" valign="top">
            <!--Add Group -->
            <table border="0" width="100%">
            <tr>
            <td>
            <table>
            <tr><td>Ad Groups  What's this?</td></tr>
            <tr><td id="no_of_adGroups">Ad Groups: 1</td></tr>
            <tr><td id="no_of_ads">Ads: 0</td></tr>
            <tr><td>
            <input type="button" name="create_ad_group" onclick="return adddiv('<?php echo $optionstring;?>');" value="Create Ad Group">
            </td></tr>
            </table>
            </td>
            </tr>
            <tr>
            <td>
            <input name="help_insert" id="help_insert" value="help_insert" type="hidden">
            <table width="100%"  border="0">
            <tbody   id="adgroup" name="adgroup">
            <tr id="adgroup0">
            <td>
            <table width="100%" name="createad" id="createad">
            <tr>
            <td>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td style="padding:0px;margin:0px;" class="txt-top">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td width="11" style="padding:0px;margin:0px;"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" /></td>
            <td class="PinkBoxTopBg" style="padding:0px;margin:0px;" class="txt-top"><img src="images/spacer.gif" alt="" width="10"  /></td>
            <td width="11" style="padding:0px;margin:0px;"><img src="images/PinkBorderTopRight.jpg" alt="" width="11"  /></td>
            </tr>
            </table>
            </td>
            </tr>
            <tr>
            <td class="PinkTwosideBorder">
            <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
            <td>
            <table width="100%">
            <tr>
            <td width="23">&nbsp;</td>
            <td width="435" id="ad_type_var"><strong>Ad Type:</strong> Text</td>
            <td width="749"> <strong>Units:</strong> CPC</td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td><strong>Ad Group Name</strong></td>
            <td><input type="text" name="add_gr_name" id="add_gr_name"></td>
            </tr>
            <tr>
            <td></td>
            <td></td>
            <td id="validate_add_gr_name" style="display:none;"></td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td><strong>Daily Budget</strong></td>
            <td><input type="text" name="daily_budget" id="daily_budget"></td>
            </tr>
            <!-- Custom Query -->
            <?php
            $query_check = "select * from temp_create_network where identity = 'main_adgroup' ";
            $res_check = mysql_query($query_check);
            $row_check = mysql_fetch_array($res_check);
            ?>
            <!-- End of custom Query -->
            <tr>
            <td>&nbsp;</td>
            <td><strong>Network</strong></td>
            <td style="width:1em;" id="adgroup_network">
                <select name="network" id="network" onchange="return custom_check('main_adgroup');" style="width: 190px;">
                <option value="">Select Network</option>
                <option value="custom" <? if(mysql_num_rows($res_check)>0){ ?> selected = "selected" <? } ?> >Custom</option>
                <?
                    while($fetNetworkAds = mysql_fetch_array($execNetworkAds)){
                ?>
                <option value="<?=$fetNetworkAds[networkId]."-".$fetNetworkAds[networkType]?>"><?php echo ucfirst($fetNetworkAds[networkName]); ?>&nbsp;$<?php echo $fetNetworkAds[networkMinPrice]; ?> </option>
                <? } ?>
                </select>
            </td>
            </tr>
            <!--Country list for iPhone-->
            <?php
                $sql_country = "select countryId, countryName from country where status='Y'";
                $res_country = mysql_query($sql_country);
            ?>
            <tr id="rCountry" style="display: none;">
                <td>&nbsp;</td>
                <td><strong>Country</strong></td>
                <td style="width:1em;" id="adgroup_country">
                    <select name="country" id="country" style="width: 190px;">
                    <?
                            while($fetCountryAds = mysql_fetch_array($res_country))
                    { ?>
                    <option value="<?php echo $fetCountryAds[countryId]?>"><?php echo $fetCountryAds[countryName]; ?></option>
                    <? } ?>
                    </select>
                </td>
            </tr>
            <tr>
            <td></td>
            <td></td>
            <td id="validate_network" style="display:none;"></td>
            </tr>
            <tr>
            <td></td>
            <td></td>
            <td id="main_adgroup_save" style="display:none;"></td>
            </tr>
            <tr>
            <td id="tdCreateAdcreatead" colspan="3" align="center"></td>
            </tr>
            <tr>
            <td></td>
            <td></td>
            <td><input type="button" name="create_ad" id="create_ad" value="Create Ad" onclick="winpopup();"></td>
            </tr>
            <tr id="show_campaign_network" style="display:none">
            <td colspan="3" align="center">Please Select The Network</td>
            </tr>
            </table>
            </td>
            </tr>
            <tr id="cRemove">
            <td align="right"><a href="javascript:void(0);" onclick="return deleteRow(0);">Remove</a>&nbsp;&nbsp;&nbsp;</td>
            </tr>

            </table>
            </td>
            </tr>
            <tr>
            <td class="txt-top">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11" /></td>
            <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11" /></td>
            <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11" /></td>
            </tr>
            </table>
            </td>
            </tr>
            </table>
            </td>
            </tr>
            </table>
            </td>
            </tr>
            </tbody>
            </table>
            <script type="text/javascript">
            removeDisplay();
            </script>
            </td>
            </tr>
        </table>
        xx
        </td>
        </tr>
        <tr id="xx"><td colspan="2"><inpUT type="submit" value="Save" id="save_all" name="save_all" onclick="return validate_main_campaign();"></td></tr>
        </table>
        </form>
                </td>
                </tr>
                <tr>
                <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                </tr>
                </table>
                </td>
                </tr>
                </table>
            </td>
        </tr>
    </table>
</td>
</tr>
</table>



<?php
        //include("footer.php");
}
?> 